<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="核查指标" prop="targetName">
        <el-input
          v-model="queryParams.targetName"
          placeholder="请输入核查指标"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="核查要点" prop="keyName">
        <el-input
          v-model="queryParams.keyName"
          placeholder="请输入核查要点"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="问题等级" prop="itemLevel">
        <el-select v-model="queryParams.itemLevel" placeholder="请选择问题等级" clearable>
          <el-option
            v-for="dict in dict.type.sys_dict_problem_level"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:item:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:item:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:item:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:item:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="itemList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="核查指标" align="center" prop="targetName" />
      <el-table-column label="核查要点" align="center" prop="keyName" />
      <el-table-column label="核查要素" align="center" prop="element" />
      <el-table-column label="核查基准数据" align="center" prop="basicInfo" />
      <el-table-column label="核查内容" align="center" prop="content" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="问题等级" align="center" prop="itemLevel">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_dict_problem_level" :value="scope.row.itemLevel"/>
        </template>
      </el-table-column>
      <el-table-column label="显示类型" align="center" prop="type">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.view_component_type" :value="scope.row.type"/>
        </template>
      </el-table-column>
      <el-table-column label="显示内容描述" align="center" prop="typeDesc" />
      <el-table-column label="是否需要上传照片" align="center" prop="needImage" width="100">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_dict_yes_no" :value="scope.row.needImage"/>
        </template>
      </el-table-column>
      <el-table-column label="排序" align="center" prop="itemSort" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:item:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:item:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改作业考核详情内容对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="60%" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="核查指标" prop="targetId">
          <el-select v-model="form.targetId" placeholder="请选择核查指标" filterable @change="selectTarget($event)">
            <el-option
              v-for="dict in targetOpts"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="核查要点" prop="keyId">
          <el-select v-model="form.keyId" placeholder="请输入核查要点"  filterable @change="selectKey($event)">
            <el-option
              v-for="dict in keyOpts"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="核查要素" prop="element">
          <el-input v-model="form.element" placeholder="请输入核查要素" />
        </el-form-item>
                <el-form-item label="核查基准数据" prop="basicInfo">
          <el-input v-model="form.basicInfo" placeholder="请输入核查基准数据" />
        </el-form-item>
        <el-form-item label="核查内容">
          <el-input v-model="form.content" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
        <el-form-item label="序号" prop="itemSort">
          <el-input v-model="form.itemSort" placeholder="请输入排序字段" />
        </el-form-item>
        <el-form-item label="问题等级" prop="itemLevel">
          <el-select v-model="form.itemLevel" placeholder="请选择问题等级" >
            <el-option
              v-for="dict in dict.type.sys_dict_problem_level"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="显示类型" prop="type">
          <el-select v-model="form.type" placeholder="请选择显示类型">
            <el-option
              v-for="dict in dict.type.view_component_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="显示内容描述" prop="typeDesc">
          <el-input v-model="form.typeDesc" placeholder="请输入显示内容描述" />
        </el-form-item>
        <el-form-item label="是否需要上传照片" prop="needImage">
          <el-select v-model="form.needImage" placeholder="请选择显示类型">
            <el-option
              v-for="dict in dict.type.sys_dict_yes_no"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listItem, getItem, delItem, addItem, updateItem } from "@/api/job/api";
import { listElementAll } from "@/api/job/element";

export default {
  name: "Item",
  dicts: ['view_component_type', 'sys_dict_problem_level','sys_dict_yes_no'],
  data() {
    return {

      jobId: null,

      type: null,

      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 作业考核详情内容表格数据
      itemList: [],

      targetOpts: [],

      keyOpts: [],


      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        targetName: null,
        keyName: null,
        content: null,
        itemLevel: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.jobId = this.$route.params.jobId;
    this.type = this.$route.params.type;

    this.getList();
  },
  methods: {
    /** 查询作业考核详情内容列表 */
    getList() {
      this.loading = true;
      listItem(this.queryParams).then(response => {
        this.itemList = response.data.records;
        this.total = response.data.total;
        this.loading = false;
      });
      listElementAll({type:this.type}).then(res => {
        console.log(res);
        this.targetOpts.splice(0, this.targetOpts.length);
        this.keyOpts.splice(0, this.keyOpts.length);
          for (let x of res.data) {
            if(x.type === 0){
              this.targetOpts.push({ value: x.id, label: x.name });
            }
            if (x.type === 1){
              this.keyOpts.push({ value: x.id, label: x.name });
            }
          }
      });
    },


    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        jobId: this.jobId,
        targetId: null,
        targetName: null,
        keyId: null,
        keyName: null,
        basicInfo: null,
        content: null,
        remark: null,
        itemSort: null,
        itemLevel: null,
        type: null,
        typeDesc: null,
        needImage: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.form.needImage = '1';
      this.form.itemSort = this.total +1;
      this.title = "添加作业考核详情内容";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getItem(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改作业考核详情内容";
      });
    },
    selectTarget(value){
      const obj = this.targetOpts.find((item)=>{ //这里的Data就是上面遍历的数据源
        return item.value === value; //筛选出匹配数据
      });
      this.form.targetName = obj.label
    },

    selectKey(value){
      const obj = this.keyOpts.find((item)=>{ //这里的Data就是上面遍历的数据源
        return item.value === value; //筛选出匹配数据
      });
      this.form.keyName = obj.label
    },

    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateItem(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addItem(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除作业考核详情内容编号为"' + ids + '"的数据项？').then(function() {
        return delItem(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/item/export', {
        ...this.queryParams
      }, `item_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
